package com.qiandw.enterprise.credit.controller

import javax.servlet.http.HttpServletRequest

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.{RequestMapping, ResponseBody}
import org.springframework.web.servlet.ModelAndView
import com.qiandw.enterprise.credit._
import com.qiandw.enterprise.credit.common.SysResp
import com.qiandw.enterprise.credit.entity.BackstageUser
import com.qiandw.enterprise.credit.service.BackstageUserSrv
import org.springframework.beans.factory.annotation.Autowired
import com.qiandw.enterprise.credit.utils._

/**
  * Created by Draven on 2018/7/9.
  * Email:hjx@qiandw.com
  */
@Controller
@RequestMapping(value = Array("/management"))
class IndexContrller extends CommonController {

  @Autowired
  var backstageUserSrv: BackstageUserSrv = _

  /**
    * 进入主界面容器
    *
    * @param request
    * @return
    */
  @RequestMapping(value = Array("/index"))
  def toIndexPage(request: HttpServletRequest): ModelAndView = {
    toPageWithAttr("/index", Array(("realname", request.getSession.getAttribute(SESSION_USER_KEY).asInstanceOf[BackstageUser].realname)))
  }

  /**
    * 修改密码
    *
    * @param request
    * @return
    */
  @RequestMapping(value = Array("/changePwd"), produces = Array("application/json;charset=UTF-8"))
  @ResponseBody()
  def changePassword(request: HttpServletRequest, oldPwd: String, newPwd: String): SysResp = {
    val user = request.getSession().getAttribute(SESSION_USER_KEY).asInstanceOf[BackstageUser]
    if (user == null) {
      SysResp(SYS_SESSION_TIME_OUT)
    } else {
      if (!user.password.equals(Md5Util.md5(oldPwd))) {
        SysResp(SYS_MSG_PWD_ERROR)
      } else {
        user.password = Md5Util.md5(newPwd)
        backstageUserSrv.updateBackstageUser(user)
        request.getSession().setAttribute(SESSION_USER_KEY,null)
        SysResp(SYS_MSG_SUCCESS)
      }
    }
  }
}
